package com.bingo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bingo.common.R;
import com.bingo.entity.Site;
import com.bingo.entity.User;
import com.bingo.entity.dto.req.ReqStatsSite;
import com.bingo.service.HisPoolService;
import com.bingo.service.CurPoolService;
import com.bingo.service.SiteService;
import com.bingo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bingo
 * @version 1.0
 * @description 场的控制类
 * @date 2024/3/21 19:18
 */
@RestController
@RequestMapping("/site")
public class SiteController {

    private HisPoolService hisPoolService;
    @Autowired
    public void setPoolService(HisPoolService hisPoolService) {
        this.hisPoolService = hisPoolService;
    }

    private SiteService siteService;
    @Autowired
    public void setSiteService(SiteService siteService) {
        this.siteService = siteService;
    }

    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //存塘统计表
    @PostMapping("/stats")
    public R siteStats(@RequestBody ReqStatsSite reqStatsSite) {
        R r = R.success(siteService.getSiteStats(reqStatsSite.getSiteNos(), reqStatsSite.getDate()));
        r.setStatusText("日期:"+reqStatsSite.getDate()+"，场的存塘情况查询成功!");
        return r;
    }

    //查询基础信息
    @GetMapping
    public R getSiteBase() {
        return R.success(siteService.getSiteBase());
    }

    //新增场
    @PostMapping("/add")
    public R addSite(@RequestBody Site site) {
        //初始为0按照添加塘的面积增加
        site.setArea(0.0);
        if (siteService.getSiteByNo(site.getSiteNo()) != null) {
            return R.error("新增场失败，场号已存在！");
        }else if (userService.getUserByNo(site.getCustodianId()) == null || userService.getUserByNo(site.getCustodianId()).getType()!=2){
            return R.error("新增场失败，不存在该负责人或该负责人不是场长！");
        }else if (siteService.save(site)) {
            User user = userService.getUserByNo(site.getCustodianId());
            R<String> r = R.success("场号: " + site.getSiteNo() + " 场名: " + site.getSiteName() + " 场长: " +user.getName());
            r.setStatusText("新增场成功！");
            return r;
        }else {
            return R.error("新增场失败！");
        }
    }

    //删除场
    @PostMapping("/delete")
    public R deleteSite(@RequestParam("siteNo") String siteNo) {
        Site site = siteService.getSiteByNo(siteNo);
        if (site == null) {
            return R.error("删除失败，不存在该场！");
        }
        if (siteService.removeById(site.getId())) {
            siteService.getPoolsBySiteNo(siteNo).forEach(poolNo -> curPoolService.deletePool(poolNo));
            return R.success("删除成功, 场号: " + siteNo + " 场名: " + site.getSiteName() + " 已删除！");
        }else {
            return R.error("删除失败！");
        }
    }

    //修改场的负责人
    @PostMapping("/updateCustodian")
    public R updateSite(@RequestBody Site site) {
        QueryWrapper<Site> wrapper = new QueryWrapper<>();
        wrapper.eq("site_no", site.getSiteNo());
        Site one = siteService.getOne(wrapper);
        if(one == null) {
            return R.error("不存在该场!");
        }
        User user = userService.getUserByNo(site.getCustodianId());
        if(user == null || user.getType() != 2) {
            return R.error("不存在该负责人或该负责人不是场长!");
        }
        one.setCustodianId(site.getCustodianId());
        siteService.updateById(one);
        return R.success("修改场长成功，当前负责人：" + user.getName());
    }

}
